import { loginAPI } from '@/apis/user'
import { setToken, getToken, removeToken } from '@/utils/auth'
import { Message } from 'element-ui'
export default {
  namespaced: true,
  state: () => {
    return {
      token: getToken() || ''
    }
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      setToken(token)
    },
    clearUserInfo(state) {
      // 清除token
      state.token = ''
      removeToken()
    }
  },
  actions: {
    async doLogin(ctx, { username, password }) {
      // 1. 调用接口
      const res = await loginAPI({ username, password })
      // 2. 提交mutation
      ctx.commit('setToken', res.data.token)
      Message.success('登录成功~')
    }
  }
}
